unit clsRepresentante;

interface

uses Dm, dbtables, biblio, SysUtils;

type
  TRepresentante = class(TObject)
  private
    Rep_Codigo: string;
  public
    Ban_Codigo: string;
    Cid_Codigo: string;
    Sup_Codigo: string;
    Rep_nome: string;
    Rep_razao: string;
    Rep_responsavel: string;
    Rep_Endereco: string;
    Rep_Bairro: string;
    Rep_Fone: string;
    Rep_Cep: string;
    Rep_NumConta: string;
    Rep_Agencia: string;
    Rep_Comissao: Double;
    Rep_Cnpj: string;
    Rep_Cpf: string;
    Rep_Inscest: string;
    Rep_Dataina: TDateTime;
    Rep_rg: string;
    Rep_Datacad: TDateTime;
    Rep_SaldoInicial: Double;
    Rep_Observacao: string;
    Rep_Celular: string;
    Rep_Email: string;
    ent_codigo: string;
    hist_codigo: string;
    Rep_FatorPerda: double;
    Tab_Codigo: string;
    Rep_Fone1: string;
    Rep_ContaBco: string;
    Rep_RPA: string;
    Tipo_ComissaoRep: string;
    Rep_Frete: Double;
    Dias_Entrega : Integer;
    function Gravar(p_Rep_Codigo: string): Byte;
    function Recuperar(p_Rep_Codigo: string): Byte;
    function Recuperar_nome(p_nome: string): Byte;
    function Recupera_ultimo: string;
    function Recupera_MovComiss(p_Rep_Codigo: string): string;
    function Ultimo: string;
    function Recupera_primeiro: string;
    function Excluir(p_Rep_Codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
    function ExisteComissaoPendente(p_Rep_Codigo: string): Boolean;
  end;

implementation

uses DB;

function TRepresentante.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Representante where Rep_Codigo=:k_Rep_Codigo');
  tq.parambyname('k_Rep_Codigo').asstring := strzero(p_Rep_Codigo, 3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Representante VALUES(:k_Rep_Codigo,');
    if Ban_Codigo <> '' then tq.sql.add(':k_Ban_codigo,') else tq.sql.add('null,');
    if Cid_Codigo <> '' then tq.sql.add(':k_Cid_Codigo,') else tq.sql.add('null,');
    if Sup_Codigo <> '' then tq.sql.add(':k_Sup_Codigo,') else tq.sql.add('null,');
    tq.sql.add(':k_Rep_nome,' +
      ':k_Rep_responsavel,' +
      ':k_Rep_Endereco,' +
      ':k_Rep_Bairro,' +
      ':k_Rep_Fone,' +
      ':k_Rep_Cep,' +
      ':k_Rep_NumConta,' +
      ':k_Rep_Agencia,' +
      ':k_Rep_Comissao,' +
      ':k_Rep_Cnpj,' +
      ':k_Rep_Cpf,' +
      ':k_Rep_Inscest,' +
      ':k_Rep_Dataina,' +
      ':k_Rep_rg,' +
      ':k_Rep_Datacad,' +
      ':k_Rep_SaldoInicial,' +
      ':k_Rep_Observacao,' +
      ':k_Rep_Celular,' +
      ':k_Rep_Email,');
    if ent_Codigo <> '' then tq.sql.add(':k_ent_Codigo,') else tq.sql.add('null,');
    if hist_codigo <> '' then tq.sql.add(':k_hist_Codigo,') else tq.sql.add('null,');
    tq.sql.add(':k_Rep_FatorPerda,' +
      ':k_Tab_Codigo,' +
      ':K_Rep_razao,' +
      ':k_Rep_Fone1,' +
      ':k_Rep_ContaBco,' +
      ':k_Rep_RPA,' +
      ':k_Tipo_ComissaoRep,' +
      ':k_Rep_Frete, '+
      ':k_Dias_Entrega)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Representante SET ');
    if Ban_Codigo <> '' then tq.sql.add('Ban_Codigo=:k_Ban_Codigo,') else tq.sql.add('Ban_Codigo=null,');
    if Cid_Codigo <> '' then tq.sql.add('Cid_Codigo=:k_Cid_Codigo,') else tq.sql.add('Cid_Codigo=null,');
    if Sup_Codigo <> '' then tq.sql.add('Sup_Codigo=:k_Sup_codigo,') else tq.sql.add('Sup_Codigo=null,');
    tq.sql.add('Rep_nome=:k_Rep_nome,' +
      'Rep_Responsavel=:k_Rep_responsavel,' +
      'Rep_Endereco=:k_Rep_Endereco,' +
      'Rep_Bairro=:k_Rep_Bairro,' +
      'Rep_fone=:k_Rep_Fone,' +
      'Rep_Cep=:k_Rep_Cep,' +
      'Rep_NumConta=:k_Rep_NumConta,' +
      'Rep_Agencia=:k_Rep_Agencia,' +
      'Rep_Comissao=:k_Rep_Comissao,' +
      'Rep_Cnpj=:k_Rep_Cnpj,' +
      'Rep_Cpf=:k_Rep_Cpf,' +
      'Rep_Inscest=:k_Rep_Inscest,' +
      'Rep_Dataina=:k_Rep_Dataina,' +
      'Rep_Rg=:k_Rep_rg,' +
      'Rep_Datacad=:k_Rep_Datacad,' +
      'Rep_SaldoInicial=:k_Rep_SaldoInicial,' +
      'Rep_Observacao=:k_Rep_Observacao,' +
      'Rep_Celular=:k_Rep_Celular,' +
      'Rep_Email=:k_Rep_Email,');
    if ent_Codigo <> '' then tq.sql.add('ent_codigo=:k_ent_Codigo,') else tq.sql.add('ent_codigo=null,');
    if hist_codigo <> '' then tq.sql.add('hist_codigo=:k_hist_Codigo,') else tq.sql.add('hist_codigo=null,');
    tq.sql.add('Rep_FatorPerda=:k_Rep_FatorPerda,' +
      'Tab_Codigo=:k_Tab_Codigo, ' +
      'Rep_Razao=:K_Rep_razao, ' +
      'Rep_Fone1=:k_Rep_Fone1, ' +
      'Rep_ContaBco=:k_Rep_ContaBco, ' +
      'Rep_Rpa=:k_Rep_RPA,' +
      'Tipo_ComissaoRep=:k_Tipo_ComissaoRep,' +
      'Rep_Frete=:k_Rep_Frete, ' +
      'Dias_Entrega=:k_Dias_Entrega '+
      'Where Rep_Codigo=:k_Rep_Codigo');
  end;
  tq.parambyname('k_Rep_Codigo').asstring := strzero(p_Rep_Codigo, 3);
  if Ban_Codigo <> '' then tq.parambyname('k_Ban_codigo').asstring := strzero(ban_codigo, 3);
  if Sup_Codigo <> '' then tq.parambyname('k_Sup_codigo').asstring := strzero(sup_codigo, 3);
  if Cid_Codigo <> '' then tq.parambyname('k_Cid_Codigo').asstring := strzero(Cid_Codigo, 4);
  tq.parambyname('k_Rep_Nome').asstring := Rep_nome;
  tq.ParamByName('k_Rep_Razao').asstring := Rep_razao;
  tq.parambyname('k_Rep_responsavel').asstring := Rep_Responsavel;
  tq.parambyname('k_Rep_endereco').asstring := Rep_endereco;
  tq.parambyname('k_Rep_bairro').asstring := Rep_Bairro;
  tq.parambyname('k_Rep_fone').asstring := Rep_Fone;
  tq.parambyname('k_Rep_cep').asstring := Rep_Cep;
  tq.parambyname('k_Rep_Numconta').asstring := Rep_NumConta;
  tq.parambyname('k_Rep_Agencia').asstring := Rep_Agencia;
  tq.parambyname('k_Rep_Comissao').asfloat := Rep_Comissao;
  tq.parambyname('k_Rep_Cnpj').asstring := Rep_Cnpj;
  tq.parambyname('k_Rep_Cpf').asstring := Rep_Cpf;
  tq.parambyname('k_Rep_Inscest').asstring := Rep_Inscest;
  tq.parambyname('k_Rep_DataIna').asdatetime := Rep_Dataina;
  tq.parambyname('k_Rep_Rg').asstring := Rep_Rg;
  tq.parambyname('k_Rep_DataCad').asdatetime := Rep_DataCad;
  tq.parambyname('k_Rep_SaldoInicial').asfloat := Rep_SaldoInicial;
  if Rep_Observacao = '' then Rep_Observacao := ' ';
  tq.parambyname('k_Rep_Observacao').AsBlob := Rep_Observacao;
  tq.parambyname('k_Rep_Celular').asstring := Rep_Celular;
  tq.parambyname('k_Rep_email').asstring := Rep_Email;
  if ent_Codigo <> '' then tq.parambyname('k_ent_Codigo').asstring := strzero(ent_Codigo, 5);
  if hist_Codigo <> '' then tq.parambyname('k_hist_Codigo').asstring := hist_codigo;
  tq.parambyname('k_Rep_FatorPerda').asfloat := Rep_FatorPerda;
  tq.parambyname('k_Tab_Codigo').asstring := Tab_Codigo;
  tq.ParamByName('k_Rep_Fone1').AsString := Rep_Fone1;
  tq.ParamByName('k_Rep_ContaBco').AsString := Rep_ContaBco;
  tq.ParamByName('k_Rep_RPA').AsString := Rep_RPA;
  tq.ParamByName('k_Tipo_ComissaoRep').AsString := Tipo_ComissaoRep;
  tq.ParamByName('k_Rep_Frete').AsFloat := REP_FRETE;
  tq.ParamByName('k_Dias_Entrega').AsInteger := Dias_Entrega;

  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TRepresentante.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Representante where Rep_Codigo=:k_Rep_Codigo');
  tq.Params[0].asstring := strzero(p_Rep_Codigo, 3);
  tq.open;
  Rep_Codigo := p_Rep_Codigo;
  if tq.eof then begin
    Ban_codigo := '';
    Sup_Codigo := '';
    Cid_Codigo := '';
    Rep_nome := '';
    Rep_razao := '';
    Rep_responsavel := '';
    Rep_Endereco := '';
    Rep_Bairro := '';
    Rep_Fone := '';
    Rep_Cep := '';
    Rep_NumConta := '';
    Rep_Agencia := '';
    Rep_Comissao := 0;
    Rep_Cnpj := '';
    Rep_Cpf := '';
    Rep_Inscest := '';
    Rep_Dataina := 0;
    Rep_rg := '';
    Rep_Datacad := date;
    Rep_SaldoInicial := 0;
    Rep_Observacao := '';
    Rep_Celular := '';
    Rep_Email := '';
    ent_codigo := '';
    hist_codigo := '';
    Rep_FatorPerda := 0;
    Tab_Codigo := '';
    Rep_Fone1 := '';
    Rep_ContaBco := '';
    Rep_RPA := '';
    Tipo_ComissaoRep := '';
    REP_FRETE := 0;
    Dias_Entrega := 0;
    Result := 0;
  end else begin
    Ban_codigo := tq.fieldbyname('Ban_Codigo').asstring;
    Sup_codigo := tq.fieldbyname('Sup_Codigo').asstring;
    Cid_Codigo := tq.fieldbyname('Cid_Codigo').asstring;
    Rep_nome := tq.fieldbyname('Rep_nome').asstring;
    Rep_razao := tq.fieldbyname('Rep_razao').asstring;
    Rep_responsavel := tq.fieldbyname('Rep_Responsavel').asstring;
    Rep_Endereco := tq.fieldbyname('Rep_Endereco').asstring;
    Rep_Bairro := tq.fieldbyname('Rep_Bairro').asstring;
    Rep_Fone := tq.fieldbyname('Rep_Fone').asstring;
    Rep_Cep := tq.fieldbyname('Rep_cep').asstring;
    Rep_NumConta := tq.fieldbyname('Rep_NumConta').asstring;
    Rep_Agencia := tq.fieldbyname('Rep_Agencia').asstring;
    Rep_Comissao := tq.fieldbyname('Rep_Comissao').asfloat;
    Rep_Cnpj := tq.fieldbyname('Rep_Cnpj').asstring;
    Rep_Cpf := tq.fieldbyname('Rep_Cpf').asstring;
    Rep_Inscest := tq.fieldbyname('Rep_Inscest').asstring;
    Rep_Dataina := tq.fieldbyname('Rep_Dataina').asdatetime;
    Rep_rg := tq.fieldbyname('Rep_Rg').asstring;
    Rep_Datacad := tq.fieldbyname('Rep_Datacad').asdatetime;
    Rep_SaldoInicial := tq.fieldbyname('Rep_SaldoInicial').asfloat;
    Rep_Observacao := tq.fieldbyname('Rep_Observacao').asstring;
    Rep_Celular := tq.fieldbyname('Rep_Celular').asstring;
    Rep_Email := tq.fieldbyname('Rep_Email').asstring;
    ent_codigo := tq.fieldbyname('ent_codigo').asstring;
    hist_codigo := tq.fieldbyname('hist_codigo').asstring;
    Rep_FatorPerda := tq.fieldbyname('Rep_FatorPerda').asfloat;
    Tab_Codigo := tq.fieldbyname('Tab_Codigo').asstring;
    Rep_Fone1 := tq.fieldbyname('Rep_Fone1').AsString;
    Rep_ContaBco := tq.fieldbyname('Rep_ContaBco').AsString;
    Rep_RPA := tq.fieldbyname('Rep_Rpa').AsString;
    Tipo_ComissaoRep := tq.fieldbyname('Tipo_ComissaoRep').AsString;
    Rep_Frete := tq.fieldbyname('Rep_Frete').AsFloat;
    Dias_Entrega := tq.fieldbyname('Dias_Entrega').AsInteger;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TRepresentante.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Representante WHERE Rep_Codigo=:k_Rep_Codigo');
  tq.parambyname('k_Rep_Codigo').asstring := strzero(p_Rep_Codigo, 3);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TRepresentante.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Representante where Rep_Nome>=:k_Rep_Nome order by Rep_Nome');
  p_q.parambyname('k_Rep_Nome').asstring := p_nome;
end;

function TRepresentante.Recupera_ultimo: string;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('select max(rep_codigo) as maior from representante');
  tq.Open;
  if tq.fieldbyname('maior').asstring <> '' then
    Result := tq.fieldbyname('maior').asstring
  else
    Result := '0';
  tq.close;
  tq.Free;
end;

function TRepresentante.Recupera_primeiro;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('select min(rep_codigo) as menor from representante');
  tq.Open;
  if tq.fieldbyname('menor').asstring <> '' then
    Result := tq.fieldbyname('menor').asstring
  else
    Result := '0';
  tq.close;
  tq.Free;
end;

function TRepresentante.Recuperar_nome(p_nome: string): Byte;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Select * from Representante where Rep_nome = ' + #39 + p_nome + #39);
  tq.open;
  if tq.Eof then
    Result := 0
  else
    Result := 1;
  tq.close;
  tq.free;
end;

function TRepresentante.Ultimo: string;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('select max(rep_codigo) as maior from representante');
  tq.Open;
  if tq.fieldbyname('maior').asstring = '' then
    Result := '001'
  else
    Result := IntToStr(tq.FieldByName('maior').AsInteger + 1);
  tq.close;
  tq.Free;
end;

function TRepresentante.Recupera_MovComiss(p_Rep_Codigo: string): string;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Representante where Rep_Codigo=:k_Rep_Codigo');
  tq.Params[0].asstring := strzero(p_Rep_Codigo, 3);
  tq.open;
  if not tq.Eof then
    Result := tq.fieldbyname('Tipo_ComissaoRep').AsString
  else
    Result := '';
  tq.Close;
  tq.Free;
end;

function TRepresentante.ExisteComissaoPendente(p_Rep_Codigo: string): Boolean;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM RepresMov where Rep_Codigo=:k_Rep_Codigo and (Repm_Liberado = ''S'' or Repm_Liberado = ''N'') ');
  tq.Params[0].asstring := strzero(p_Rep_Codigo, 3);
  tq.open;
  Result := not tq.Eof;
  tq.Close;
  tq.Free;
end;

end.
